//
//  WSDiscoveryViewController.m
//  GroupFourFM
//
//  Created by dllo on 15/12/22.
//  Copyright © 2015年 GroupFour. All rights reserved.
//

#import "WSDiscoveryViewController.h"
#import "Masonry.h"
#import "WSRecommendViewController.h"
#import "WSListViewController.h"
#import "WSAuthorViewController.h"
#import "WSSearchViewController.h"

@interface WSDiscoveryViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *bigScrollView;

@property (nonatomic, strong) NSMutableArray *btnArray;

@property (nonatomic, strong) UIView *selectView;

@property (nonatomic, retain) UIButton *beforeButton;

@property (nonatomic, retain) UICollectionView *collectionView;

@end

@implementation WSDiscoveryViewController

- (void)viewWillAppear:(BOOL)animated {

    self.title = @"发现";
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.btnArray = [NSMutableArray array];
    self.view.backgroundColor = [UIColor colorWithRed:0.98 green:0.98 blue:0.98 alpha:1];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"search_btn_hl"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(rightAction:)];
    
    [self createScrollView];
    [self createBigScrollView];
}

- (void)createBigScrollView {

    self.bigScrollView = [[UIScrollView alloc] init];
    self.bigScrollView.backgroundColor = COLOR_BG;
    [self.view addSubview:self.bigScrollView];
//    [_bigScrollView release];
//    self.bigScrollView.contentSize = CGSizeMake(SCREEN_WIDTH * 3, SCREEN_HEIGHT - 152);
    self.bigScrollView.pagingEnabled = YES;
    self.bigScrollView.bounces = NO;
    self.bigScrollView.delegate = self;
    [self.bigScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(@40);
        make.left.equalTo(self.view);
        make.size.equalTo(self.view).sizeOffset(CGSizeMake(0, -40));
    }];
    
    UIView *scrollV = [[UIView alloc] init];
    [self.bigScrollView addSubview:scrollV];
    
    UIView *lineUp = [[UIView alloc] init];
    lineUp.backgroundColor = [UIColor colorWithRed:0.87 green:0.87 blue:0.87 alpha:1];
    [self.view addSubview:lineUp];
    [lineUp mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.left.mas_equalTo(@0);
        make.top.mas_equalTo(@0);
        make.height.mas_equalTo(@1);
    }];
    
    UIView *lineDown = [[UIView alloc] init];
    lineDown.backgroundColor = [UIColor colorWithRed:0.87 green:0.87 blue:0.87 alpha:1];
    [self.view addSubview:lineDown];
    [lineDown mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.left.mas_equalTo(@0);
        make.top.mas_equalTo(@39);
        make.height.mas_equalTo(@1);
    }];

    //推荐页面
    WSRecommendViewController *myTableVC = [[WSRecommendViewController alloc] init];
    [self addChildViewController:myTableVC];
    [scrollV addSubview:myTableVC.view];
    
    [myTableVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.top.mas_equalTo(@0);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT - 152));
    }];

    //榜单页面
    WSListViewController *listVC = [[WSListViewController alloc] init];
    [self addChildViewController:listVC];
    [scrollV addSubview:listVC.view];
    
    [listVC.view mas_makeConstraints:^(MASConstraintMaker *make) {

        make.top.bottom.equalTo(@0);
        make.left.equalTo(myTableVC.view.mas_right);
        make.height.equalTo(myTableVC.view.mas_height);
        make.width.equalTo(myTableVC.view.mas_width);
    }];

    //主播页面
    WSAuthorViewController *authorVC = [[WSAuthorViewController alloc] init];
    [self addChildViewController:authorVC];
    [scrollV addSubview:authorVC.view];
    
    [authorVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.bottom.equalTo(@0);
        make.left.equalTo(listVC.view.mas_right);
        make.height.equalTo(listVC.view.mas_height);
        make.width.equalTo(listVC.view.mas_width);
    }];
    
    [scrollV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(self.bigScrollView);
        
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH * 3, SCREEN_HEIGHT - 152));
        
    }];

}

- (void)createScrollView {

    NSArray *array = [NSArray arrayWithObjects:@"推荐", @"榜单", @"主播", nil];
    
    for (int i = 0; i < 3; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        [button setTitle:array[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        button.tag = 1000 + i;
        [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
        [self.btnArray addObject:button];
        
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.equalTo(@0);
            make.left.mas_equalTo(@(SCREEN_WIDTH / 3 * i));
            make.width.mas_equalTo(@(SCREEN_WIDTH / 3));
            make.height.mas_equalTo(@40);
        }];
    }
    
    self.selectView = [[UIView alloc] init];
    self.selectView.backgroundColor = [UIColor colorWithRed:0.156 green:0.803 blue:0.607 alpha:1.000];
    [self.view addSubview:self.selectView];
    
    [self.selectView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(@36);
        make.left.mas_equalTo(@(SCREEN_WIDTH / 12));
        make.width.mas_equalTo(@(SCREEN_WIDTH / 6));
        make.height.mas_equalTo(@3);
    }];
}

#pragma mark - 导航按钮点击事件
- (void)buttonAction:(UIButton *)button {
    
    if (![button isEqual:self.beforeButton]) {
        
        //设置button大小和滑块位置
        NSInteger index = button.tag - 1000;
        [self.bigScrollView setContentOffset:CGPointMake(index * SCREEN_WIDTH, 0) animated:YES];
        
        [self.beforeButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        CGFloat x = (button.tag - 1000) * (SCREEN_WIDTH / 3) + (SCREEN_WIDTH / 12);
        
        [self.selectView mas_updateConstraints:^(MASConstraintMaker *make) {
            
            make.left.mas_equalTo(@(x));
        }];
        
        [UIView animateWithDuration:0.2 animations:^{
            
            [self.view layoutIfNeeded];
            self.beforeButton.transform = CGAffineTransformMakeScale(1, 1);
            button.transform = CGAffineTransformMakeScale(1.15, 1.15);
        }];
        [button setTitleColor:COLOR_SELECTED forState:UIControlStateNormal];
        self.beforeButton = button;
        
    }
}

#pragma mark 搜索
- (void)rightAction:(UIBarButtonItem *)bar {

    WSSearchViewController *searchVC = [[WSSearchViewController alloc] init];
    WSTabBarController *tabbarC = (WSTabBarController *)self.tabBarController;
    [tabbarC isHiddenTabBar:YES withViewController:searchVC];
    [self.navigationController pushViewController:searchVC animated:YES];
}

#pragma mark 设置导航
- (void)setSelectNum:(NSInteger)selectNum {
    
    UIButton *button = [self.btnArray objectAtIndex:selectNum];
    
    if (![button isEqual:self.beforeButton]) {
        
        [self.beforeButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        CGFloat x = selectNum * (SCREEN_WIDTH / 3) + (SCREEN_WIDTH / 12);
        
        [self.selectView mas_updateConstraints:^(MASConstraintMaker *make) {
            
            make.left.mas_equalTo(@(x));
        }];
        
        [UIView animateWithDuration:0.2 animations:^{
            
            [self.view layoutIfNeeded];
            self.beforeButton.transform = CGAffineTransformMakeScale(1, 1);
            button.transform = CGAffineTransformMakeScale(1.15, 1.15);
        }];
        [button setTitleColor:COLOR_SELECTED forState:UIControlStateNormal];
        self.beforeButton = button;
    }
    
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    NSInteger index = scrollView.contentOffset.x / SCREEN_WIDTH;
    [self setSelectNum:index];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
